<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class hoidonghuong_category extends CI_Controller
{
	protected $_templates;
	
	function __construct() 
	{
		parent::__construct();
		$this->pre_message = "";         
		/*$this->permit_library->check_permit();
		$this->session->set_userdata(array('Url'=>uri_string()));*/
		//load language
		$this->load->helper('search') ;
		$this->lang->load('hoidonghuong');
		$this->load->library('string_library');
		$this->load->model('hoidonghuong_category_model','hoidonghuong_category');
	}   
	
	function index()
	{
		/*Begin search*/
		$type = $this->input->post('type');
		$data['search_bar']= searchBar(array(),array('catname'=>'Category Name'),'hoidonghuong_category/index/');
		if($type=='ajax')
		{
			$arr_search['key_search'] = $this->input->post('key_search');
			$arr_search['field_search'] = $this->input->post('field_search');
			$this->session->set_userdata('key_search',$arr_search['key_search']);
			$this->session->set_userdata('field_search',$arr_search['field_search']);
		}
		else
		{
			$arr_search['key_search']=$this->session->userdata('key_search');
			$arr_search['field_search']=$this->session->userdata('field_search');
		}
		/*End  search*/
		
		$data['title'] = 'Danh sách hội đồng hương';
		$data['add'] = 'hoidonghuong_category/add';
		$config['base_url'] = base_url().'hoidonghuong_category/index/'.(int)$this->uri->segment(3);  
		$data['num'] = $this->hoidonghuong_category->get_num_data($arr_search);
		$config['total_rows'] =  $data['num'];
		$config['per_page'] = 10;
		$config['uri_segment'] = 4;   
		$this->pagination->initialize($config); 
		  
		$data['list'] =   $this->hoidonghuong_category->get_all_data($config['per_page'],(int)$this->uri->segment(4),$arr_search);
		$data['pagination']    = $this->pagination->create_links();            
	    
		$data['page'] = 'hoidonghuong/category/showlist' ;
		$this->load->view('layout/skin', $data);
	}
	
	function add()
	{                       
		$data['title'] =  lang('title_add');
		$data['option_pid'] = $this->hoidonghuong_category->getCategory();
		
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('name_vn',lang('dsq_category_name'),'trim|required');
			if($this->form_validation->run() == FALSE){
				$this->pre_message = validation_errors();
			}else{
				$data = $this->build_data();
				if($this->hoidonghuong_category->save($data)){
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('hoidonghuong_category/index');
				}else{
					$this->pre_message = 'Lưu không thành công';
					redirect('hoidonghuong_category/add');
				}
			}
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'hoidonghuong/category/add';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function edit($id)
	{                         
		$data['title'] = lang('title_edit');
		$data['rs'] = $this->hoidonghuong_category->get_object($id);
		$data['option_pid'] = $this->hoidonghuong_category->getCategory($data['rs']->parentid);
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('name_vn',lang('dsq_category_name'),'trim|required');
			if($this->form_validation->run() == FALSE){
				$this->pre_message = validation_errors();
			}else{
				$data = $this->build_data();
				if($this->hoidonghuong_category->save($data,$id)){
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('hoidonghuong_category/index');
				}else{
					$this->pre_message = 'Lưu không thành công';
					redirect('hoidonghuong_category/edit');
				}
			}
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'hoidonghuong/category/edit';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function del()
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		
		if($this->hoidonghuong_category->deleted($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
			redirect('news_category/index/'.$page);
	}
	
	function dels()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
		
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->hoidonghuong_category->deleted($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('news_category/index/'.$page);
	}
	
	function build_data($flag=1)
	{
		$req["parentid"] = $this->input->post("parentid",0);
		$req["catname"] = $this->input->post("name_vn");
		// get image
		/*$catlogo = $_FILES['logo'];
		if($catlogo['error']==0){
			$this->load->library('upload',DAISUQUAN);
			$strimage=$this->upload->do_upload($catlogo);
			// resize
			$name=$this->upload->getName($strimage);
			$this->upload->resize($strimage,DAISUQUAN_SMALL.'/'.$name,'landscape',30);
		}
		$image=$this->input->post("editlogo",'');
		$req["catlogo"]=!empty($strimage)?$strimage:$image;*/
		
		$req["order"] = $this->input->post("nb_order");
		$req["bl_active"] = (int) $this->input->post("bl_active");
		$req["create"] = date('Y-m-d');
		
		//upload hinh anh
		$img = $this->input->post("catlogo");
		$img_name = end(explode('/', $img)) ;
		if($img)
		{
			if(file_exists('../uploads/hoidonghuong/'.$img_name))
			{
				unlink('../uploads/hoidonghuong/'.$img_name);
			}
			fn_resize_image('../uploads/'.$img,'../uploads/hoidonghuong/'.$img_name, 90, 90); 
			$req["catlogo"] = $img_name;
		}
		return $req;
	}
	
}
?>
